

const  requestMode = require('../../../js/requestsMode.js');
const session = require('../../../js/session.js');
let modelImgUrl = "../../../images/";
var tcity = require("../../../js/citys.js");
var Tools = require("../../../js/Tools.js");
const app = getApp();
Page({
    data :{
        rightArrow:modelImgUrl+"rightArrow_black.png",
        provinces: [],
        name:'',
        phone:'',
        detailedAddress:'',
        province: "",
        citys: [],
        city: "",
        countys: [],
        county: '',
        value: [22,0,0],
        values: [22, 0, 0],
        lastValues:[22, 0, 0],
        condition: false,
        ifHasDefaultAddress:true,
        lastProvince: '',
        lastCity:'',
        lastCounty:'',
        addressArray : [],
        currentIndex : 0
    },
    bindChange: function(e) {
        var val = e.detail.value;
        var t = this.data.values;
        var cityData = this.data.cityData;
        if(val[0] != t[0]){
            const citys = [];
            const countys = [];
            for (let i = 0 ; i < cityData[val[0]].sub.length; i++) {
                citys.push(cityData[val[0]].sub[i].name)
            }
            for (let i = 0 ; i < cityData[val[0]].sub[0].sub.length; i++) {
                countys.push(cityData[val[0]].sub[0].sub[i].name)
            }
            this.setData({
                province: this.data.provinces[val[0]],
                city: cityData[val[0]].sub[0].name,
                citys:citys,
                county: cityData[val[0]].sub[0].sub[0].name,
                countys:countys,
                values: val,
                value:[val[0],0,0]
            });
        }
        if(val[1] != t[1]){
            const countys = [];
            for (let i = 0 ; i < cityData[val[0]].sub[val[1]].sub.length; i++) {
                countys.push(cityData[val[0]].sub[val[1]].sub[i].name)
            }
            this.setData({
                city: this.data.citys[val[1]],
                county: cityData[val[0]].sub[val[1]].sub[0].name,
                countys:countys,
                value:[val[0],val[1],0]
            });
        }
        if(val[2] != t[2]){
            this.setData({
                county: this.data.countys[val[2]],
                values: val,
                value:val
            });
        }
    },
    cancel:function(){
        let lastValues = this.data.lastValues;
        let This = this;
        this.setData({
            condition:!this.data.condition,
            /* ifHasDefaultAddress:true,*/
            value:lastValues,
            values:lastValues,
            lastValues:lastValues,
            /*----*/
            province: This.data.lastProvince,
            city:This.data.lastCity,
            county:This.data.lastCounty,
            /*----*/
            lastProvinces: This.data.lastProvinces,
            lastCitys:This.data.lastCitys,
            lastCountys:This.data.lastCountys,

            provinces:this.data.lastProvinces,
            citys:This.data.lastCitys,
            countys:This.data.countys
        })
    },
    open:function(){
        this.setData({
            condition:!this.data.condition
        })
    },
    sure:function(){
        let This = this;
        let choseValues = This.data.value;
        let citydata= This.data.cityData;
        let submitProvinceId = citydata[choseValues[0]].code;
        let submitCityId = citydata[choseValues[0]]['sub'][choseValues[1]].code;
        let submitAreaId = citydata[choseValues[0]]['sub'][choseValues[1]]['sub'][choseValues[2]].code;
        let submitChoseAddress = [submitProvinceId,submitCityId,submitAreaId];
        This.setData({
            condition:!This.data.condition,
            ifHasDefaultAddress:true,
            lastValues:choseValues,
            value:choseValues,
            values:choseValues,
            province: This.data.province,
            city:This.data.city,
            county:This.data.county,
            lastProvince: This.data.province,
            lastCity:This.data.city,
            lastCounty:This.data.county,
            submitChoseAddress:submitChoseAddress,
            lastProvinces: This.data.provinces,
            lastCitys:This.data.citys,
            lastCountys:This.data.countys
        })
    },
    onLoad: function (getId) {
        let that = this;
        var addressArr = JSON.parse(getId.addressArr);
        let currentId = addressArr[getId.index].id;
        that.setData({
            address_id:currentId,
            currentIndex : getId.index,
            addressArray : addressArr
        });
        tcity.init(that);
        var cityData = that.data.cityData;
        const provinces = [];
        const citys = [];
        const countys = [];
        let thisAddressId = currentId;
        let proviceId = '';
        let cityId = '';
        let areaId = '';
        let getData = '';
        let address_id = '';
        requestMode.sendRequest('address/detail','POST',{address_id:thisAddressId},{sKey:session.get('sKey')}).then(function(res){
                let code = res.data.code,getData = res.data.data;
           if(code == 200){
               getData = res.data.data;
               address_id = getData.address_id;
               proviceId = getData.provinceid;
               cityId = getData.cityid;
               areaId = getData.areaid;
               for(let i=0;i<cityData.length;i++){
                   provinces.push(cityData[i].name);
               }
               let provinceCurrent = '',cityCurrent = '',areaCurrent = '';
               for(let i in cityData){
                   if( cityData[i]['code'] == proviceId){
                       provinceCurrent = i;break;
                   }
               }
               for (let i = 0 ; i < cityData[provinceCurrent].sub.length; i++) {
                   citys.push(cityData[provinceCurrent].sub[i].name)
               }
               for( let i in cityData[provinceCurrent]['sub']){
                   if( cityData[provinceCurrent]['sub'][i]['code'] == cityId){
                       cityCurrent = i;
                   }
               }
               for (let i = 0 ; i < cityData[provinceCurrent]['sub'][cityCurrent]['sub'].length; i++) {
                   countys.push(cityData[provinceCurrent].sub[cityCurrent].sub[i].name)
               }
               for( let i in cityData[provinceCurrent]['sub'][cityCurrent]['sub']){
                   if( cityData[provinceCurrent]['sub'][cityCurrent]['sub'][i]['code'] == areaId){
                       areaCurrent = i;
                   }
               }
               that.setData({
                   'provinces': provinces,
                   'citys':citys,
                   'countys':countys,
                   'province':cityData[provinceCurrent].name,
                   'city':cityData[provinceCurrent].sub[cityCurrent].name,
                   'county':cityData[provinceCurrent].sub[cityCurrent].sub[areaCurrent].name,
                   lastProvince: cityData[provinceCurrent].name,
                   lastCity:cityData[provinceCurrent].sub[cityCurrent].name,
                   lastCounty:cityData[provinceCurrent].sub[cityCurrent].sub[areaCurrent].name,
                   lastProvinces: provinces,
                   lastCitys:citys,
                   lastCountys:countys,
                   name:getData.consignee,
                   phone:getData.mobile,
                   detailedAddress:getData.street,
                   value: [provinceCurrent,cityCurrent,areaCurrent],
                   values: [provinceCurrent,cityCurrent,areaCurrent],
                   lastValues:[provinceCurrent,cityCurrent,areaCurrent],
                   submitChoseAddress:[proviceId,cityId,areaId]
               });
           }else{
               Tools.showErrorToast(res.data.message)
           }
        },function(){
            Tools.showWrongNetWorkToast();
        });
    },
    namechange:function(e){
        this.setData({
            name: e.detail.value
        })
    },
    phonechange:function(e){
        this.setData({
            phone: e.detail.value
        });
    },
    detailedAddressChange:function(e){
        this.setData({
            detailedAddress: e.detail.value
        });
    },
    submit:function(){
        let This = this;
        let postAddressId = This.data.submitChoseAddress;
        let postData={
            provinceid:postAddressId[0],
            cityid:postAddressId[1],
            areaid:postAddressId[2],
            street:This.data.detailedAddress,
            consignee:This.data.name,
            mobile:This.data.phone,
            address_id:This.data.address_id
        };
        requestMode.sendRequest('address/modify','POST',postData,{'sKey':session.get('sKey')}).then(function(res){
            if(res.data.code == 200 ){
                //wx.showToast({
                //    title:'修改成功',
                //    icon:'success',
                //    duration:800
                //});
                Tools.showSuccessInfoToast("修改成功");
                This.updateLastPageInfo();
                setTimeout(function(){
                    wx.navigateBack({
                        delta: 1
                    })
                },800)
            }else{
                Tools.showErrorToast(res.data.message)
            }
        },function(){
            Tools.showWrongNetWorkToast();
            /*111111*/
        })
    },
    updateLastPageInfo : function () {
        let fullAddress = this.data.province + "-" + this.data.city + "-" + this.data.county + this.data.detailedAddress;
        var currentPages = getCurrentPages();
        if (currentPages.length >= 2) {
            let lastPage = currentPages[currentPages.length - 2];
            if ("pages/mines/addressManager/addressManager" == lastPage.__route__) {
                ///如果上一个界面是地址管理
                ///给上一个界面重新设置数值 并且刷新上一个界面
                var currentAddress = this.data.addressArray[this.data.currentIndex];
                currentAddress.address =fullAddress ;
                currentAddress.name = this.data.name;
                currentAddress.phone = this.data.phone;
                this.data.addressArray[this.data.currentIndex] = currentAddress;
                lastPage.setData({
                        addressList : this.data.addressArray
                    }
                );
            }
        }
    },
    onHide:function(){

    }
});
